package com.mianyang.zhangsan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mianyang.zhangsan.common.util.OperationManHelper;
import com.mianyang.zhangsan.common.util.StringUtils;
import com.mianyang.zhangsan.exception.SecServiceException;
import com.mianyang.zhangsan.model.entity.SecVideoLog;
import com.mianyang.zhangsan.mapper.SecVideoLogMapper;
import com.mianyang.zhangsan.service.ISecVideoLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jiangjr
 * @since 2022-10-23
 */
@Service
public class SecVideoLogServiceImpl extends ServiceImpl<SecVideoLogMapper, SecVideoLog> implements ISecVideoLogService {

    @Override
    public void saveVideoLog(SecVideoLog log) {
        //记录当前的用户信息
        SecVideoLog before = before(log);
        save(before);
    }

    @Override
    public void updateVideoLog(SecVideoLog log) {
        SecVideoLog before = before(log);
        updateById(before);
    }

    private SecVideoLog before(SecVideoLog log) {
        log.setAccountId(OperationManHelper.getAccountId());
        log.setAccountName(OperationManHelper.getAccountName());
        if (log.getReadTime() >= log.getFileTime()) {
            log.setStudyStatus(1);
        } else {
            log.setStudyStatus(2);
        }
        log.setUpdateTime(new Date());
        return log;
    }

    @Override
    public Page<SecVideoLog> selectVideoLogs(SecVideoLog videoLog, int current, int pageSize) {
        QueryWrapper<SecVideoLog> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(videoLog.getFileName())) {
            wrapper.like("file_name", videoLog.getFileName());
        }
        if (StringUtils.isNotEmpty(videoLog.getAccountName())) {
            wrapper.like("account_name", videoLog.getAccountName());
        }
        if (videoLog.getStudyStatus() != null) {
            wrapper.eq("study_status", videoLog.getStudyStatus());
        }
        return page(new Page<>(current, pageSize), wrapper);
    }

    @Override
    public void updateAdaptor(SecVideoLog log) {
        if (log.getFileId() == null) {
            throw new SecServiceException("未找到相关视频");
        }
        QueryWrapper<SecVideoLog> wrapper = new QueryWrapper<>();
        wrapper.eq("file_id", log.getFileId());
        wrapper.eq("account_id", OperationManHelper.getAccountId());
        // 查询是否存在记录
        SecVideoLog videoLog = getOne(wrapper);
        if (videoLog == null) {
            // 不存在记录，新增条记录
            saveVideoLog(log);
        } else {
            // 更新记录信息
            updateVideoLog(log);
        }
    }

    @Override
    public SecVideoLog selectVideoByFileId(Long fileId) {
        QueryWrapper<SecVideoLog> wrapper = new QueryWrapper<>();
        if (fileId != null) {
            wrapper.eq("file_id", fileId);
            wrapper.eq("account_id", OperationManHelper.getAccountId());
            return getOne(wrapper);
        }
        return null;
    }
}
